using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public enum E_UILayer
{
    Bottom,
    Mid,
    Top,
    System,
}

public class UIManager:SingletonPattern<UIManager>
{
    public UIManager() 
    {
        GameObject canvasObj = GameObject.Instantiate<GameObject>(ResourceLoadManager.Instance.Load<GameObject>("UI/Panel/Canvas"));
        canvas = canvasObj.transform as RectTransform;
        GameObject eventObj = GameObject.Instantiate<GameObject>(ResourceLoadManager.Instance.Load<GameObject>("UI/Panel/EventSystem"));

        bottom = canvas.Find("Bottom");
        mid = canvas.Find("Mid");
        top = canvas.Find("Top");
        system = canvas.Find("System");


        GameObject.DontDestroyOnLoad(canvasObj);
        GameObject.DontDestroyOnLoad(eventObj);
    }

    private Transform bottom;
    private Transform mid;
    private Transform top;
    private Transform system;

    public RectTransform canvas;

    public Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();
    public void ShowPanel<T>(string name,E_UILayer UILayer=E_UILayer.Bottom,UnityAction<T> callBack=null)where T:BasePanel
    {
        if (panelDic.ContainsKey(name)) return;

        ResourceLoadManager.Instance.LoadAsync<GameObject>("UI/Panel/" + name, (panel) =>
        {
            GameObject panelObj = null;
            switch (UILayer)
            {
                case E_UILayer.Bottom:
                    panelObj = GameObject.Instantiate(panel, bottom);
                    break;
                case E_UILayer.Mid:
                    panelObj = GameObject.Instantiate(panel, mid);
                    break;
                case E_UILayer.Top:
                    panelObj = GameObject.Instantiate(panel, top);
                    break;
                case E_UILayer.System:
                    panelObj = GameObject.Instantiate(panel, system);
                    break;
            }

            T Panel = panelObj.GetComponent<T>();
            if (callBack!=null)
            {
                callBack(Panel);
            }

            Panel.ShowMe();

            panelDic.Add(name, Panel);
        });
    }
    public void HidePanel(string name)
    {
        if (panelDic.ContainsKey(name))
        {
            panelDic[name].HideMe();

            GameObject.DestroyImmediate(panelDic[name].gameObject, true);
            //GameObject.Destroy(panelDic[name].gameObject);
            
            panelDic.Remove(name);
        }
    }
    public T GetPanel<T>(string panelNmae)where T:BasePanel
    {
        if (panelDic.ContainsKey(panelNmae))
        {
            return panelDic[panelNmae] as T;
        }
        return null;
    }
    public Transform GetLayerFather(E_UILayer layer)
    {
        switch (layer)
        {
            case E_UILayer.Bottom:
                return bottom;
            case E_UILayer.Mid:
                return mid;
            case E_UILayer.Top:
                return top;
            case E_UILayer.System:
                return system;
        }
        return null;
    }
    
    public static void AddCustomEventListener(UIBehaviour control,EventTriggerType type,UnityAction<BaseEventData> callBack)
    {
        EventTrigger trigger = control.GetComponent<EventTrigger>();
        EventTrigger.Entry entry = new EventTrigger.Entry();
        entry.eventID = type;
        entry.callback.AddListener(callBack);

        trigger.triggers.Add(entry);
    }

}
